﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.TES.Entity.FunctionEntity;
using ZDSoft.TES.Core;
using ZDSoft.TES.Service.FunctionService;
using ZDSoft.TES.Web.Helpers;
using NHibernate.Criterion;

namespace ZDSoft.TES.Web.Controllers
{
    public class RuleController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult getRules()
        {
            IList<Rule> rules = Container.Instance.Resolve<IRuleService>().FindAll();
            IList<ZTreeHelper<Quota>.ZTreeNode> tree = new List<ZTreeHelper<Quota>.ZTreeNode>();
            foreach (Rule entity in rules)
            {
                ZTreeHelper<Quota>.ZTreeNode node = new ZTreeHelper<Quota>.ZTreeNode();
                node.id = entity.ID;
                node.tld = entity.Remark;
                node.pid = 0;
                node.name = entity.RuleName;
                tree.Add(node);
            }
            return this.Json(tree, JsonRequestBehavior.AllowGet);
        }

        public void Opreate(int id, string name, string remark, string pId, string checkedIDs)
        {
            string[] ids = checkedIDs.Split(',');
            if (id == 0)
            {
                Rule rule = new Rule();
                rule.RuleName = name;
                rule.Remark = remark;
                rule.Quotas = new List<Quota>();
                foreach (string i in ids)
                {
                    rule.Quotas.Add(Container.Instance.Resolve<IQuotaService>().FindByID(int.Parse(i)));
                }
                Container.Instance.Resolve<IRuleService>().Create(rule);
            }
            else
            {
                Rule rule = Container.Instance.Resolve<IRuleService>().FindByID(id);
                rule.RuleName = name;
                rule.Remark = remark;
                rule.Quotas = null;
                rule.Quotas = new List<Quota>();
                foreach (string i in ids)
                {
                    rule.Quotas.Add(Container.Instance.Resolve<IQuotaService>().FindByID(int.Parse(i)));
                }
                Container.Instance.Resolve<IRuleService>().Update(rule);
            }
        }

        public string getEntiyID(string name, string remark)
        {
            IList<ICriterion> conditions = new List<ICriterion>();
            conditions.Add(Expression.Eq("RuleName", name));
            conditions.Add(Expression.Eq("Remark", remark));
            Rule rule = Container.Instance.Resolve<IRuleService>().Find(conditions)[0];
            return rule.ID.ToString() + "," + rule.Remark.ToString();
        }

        public void Delete(int id)
        {
            Container.Instance.Resolve<IRuleService>().Delete(id);
        }

        public JsonResult getQuotas(int id)
        {
            if (id == 0)
            {
                IList<Quota> list = Container.Instance.Resolve<IQuotaService>().FindAll();
                IList<ZTreeHelper<Quota>.ZTreeNode> tree = new List<ZTreeHelper<Quota>.ZTreeNode>();
                foreach (Quota entity in list)
                {
                    if (entity.Parent == null)
                    {
                        ZTreeHelper<Quota>.ZTreeNode node = new ZTreeHelper<Quota>.ZTreeNode();
                        node.id = entity.ID;
                        node.tld = entity.Percentage.ToString();
                        node.isParent = true;
                        node.pid = 0;
                        node.name = entity.QuotaName;
                        node.children = new List<ZTreeHelper<Quota>.ZTreeNode>();
                        foreach (Quota chi in entity.Children)
                        {
                            ZTreeHelper<Quota>.ZTreeNode child = new ZTreeHelper<Quota>.ZTreeNode();
                            child.id = chi.ID;
                            child.pid = chi.Parent.ID;
                            child.tld = entity.Percentage.ToString();
                            child.name = chi.QuotaName;
                            child.isParent = true;
                            node.children.Add(child);
                        }
                        tree.Add(node);
                    }
                }
                return this.Json(tree, JsonRequestBehavior.AllowGet);
            }
            else
            {
                IList<ZTreeHelper<Quota>.ZTreeNode> tree = new List<ZTreeHelper<Quota>.ZTreeNode>();
                IList<Quota> rulesQ = Container.Instance.Resolve<IRuleService>().FindByID(id).Quotas;
                IList<Quota> list = Container.Instance.Resolve<IQuotaService>().FindAll();
                foreach (Quota i in list)
                {
                    if (i.Parent == null && rulesQ.Contains(i))
                    {
                        ZTreeHelper<Quota>.ZTreeNode node = new ZTreeHelper<Quota>.ZTreeNode();
                        node.id = i.ID;
                        node.pid = 0;
                        node.name = i.QuotaName;
                        node.@checked = true;
                        if (i.Children.Count != 0)
                        {
                            node.children = new List<ZTreeHelper<Quota>.ZTreeNode>();
                            foreach (Quota j in list)
                            {
                                if (j.Parent != null && i.Children.Contains(j))
                                {
                                    ZTreeHelper<Quota>.ZTreeNode sub = new ZTreeHelper<Quota>.ZTreeNode();
                                    sub.id = j.ID;
                                    sub.pid = j.Parent.ID;
                                    sub.name = j.QuotaName;
                                    sub.@checked = true;
                                    node.children.Add(sub);
                                }
                            }
                        }
                        tree.Add(node);
                    }
                    else if (i.Parent == null && !rulesQ.Contains(i))
                    {
                        ZTreeHelper<Quota>.ZTreeNode node = new ZTreeHelper<Quota>.ZTreeNode();
                        node.id = i.ID;
                        node.pid = 0;
                        node.name = i.QuotaName;
                        if (i.Children.Count != 0)
                        {
                            node.children = new List<ZTreeHelper<Quota>.ZTreeNode>();
                            foreach (Quota j in i.Children)
                            {
                                ZTreeHelper<Quota>.ZTreeNode sub = new ZTreeHelper<Quota>.ZTreeNode();
                                sub.id = j.ID;
                                sub.pid = j.Parent.ID;
                                sub.name = j.QuotaName;
                                node.children.Add(sub);
                            }
                        }
                        tree.Add(node);
                    }
                }
                return this.Json(tree, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
